package xiaomi

import (
	"dta-apk/global"
	"dta-apk/utils"
	"encoding/json"
	"fmt"
)

// 应用详情
func GetAppDetails(packageName, userName string) (*XiaoMiAppInfo, bool) {
	utils.Info("获取小米应用详情")
	// 请求参数
	paramMap := map[string]string{
		"packageName": packageName,
		"userName":    userName,
	}
	// 签名
	sign, error := sign(paramMap, nil)
	if error != nil {
		panic(error)
	}
	// 请求接口参数
	paramJson, _ := json.Marshal(paramMap)
	requestMap := map[string]string{
		"RequestData": string(paramJson),
		"SIG":         sign,
	}
	result := utils.PostFormHeader(getApiUrl(api_query), requestMap, getHeaders())
	utils.Info("小米应用详情:" + result)
	var appInfo XiaoMiAppInfo
	error = json.Unmarshal([]byte(result), &appInfo)
	if error != nil {
		utils.Info("解析json失败:" + error.Error())
	} else {
		if appInfo.Result == 0 {
			return &appInfo, true
		}
	}

	return nil, false

}

// 创建应用
func CreateApp(packageName, appName string) {
	appInfo := AppPushAppInfo{
		AppName:     appName,
		PackageName: packageName,
		Category:    4, // 居家生活
		KeyWords:    "订餐 外卖 聊天 社交",
		Desc:        "",
		ShortDesc:   "",
		PrivacyUrl:  "",
	}
	appInfoJson, _ := json.Marshal(appInfo)
	pushInfo := AppPushInfo{
		UserName:    global.Config.App.XiaoMi.UserName,
		SynchroType: "0", // 新增应用
		AppInfo:     string(appInfoJson),
	}
	// 签名
	sign, _ := sign(utils.StructToStringMap(pushInfo), nil)
	// 请求参数
	paramJson, _ := json.Marshal(pushInfo)
	requestMap := map[string]interface{}{
		"RequestData": string(paramJson),
		"SIG":         sign,
	}

	result := utils.PostBodyHeader(getApiUrl(api_push), requestMap, getHeaders())
	fmt.Println(result)
}

// apk升级应用
func UpdateApk(packageName, appName, updateDesc, apkFile string) (bool, string) {
	appDetail, isS := GetAppDetails(packageName, global.Config.App.XiaoMi.UserName)
	if isS {
		if !appDetail.UpdateVersion {
			msg := "小米平台不允许应用版本更新"
			utils.Info(msg)
			return false, msg
		}
	}
	utils.Info("发布升级申请到小米应用")
	// appInfo := AppPushAppInfo{
	// 	AppName:     appName,
	// 	PackageName: packageName,
	// 	UpdateDesc:  updateDesc,
	// }
	appInfo := map[string]string{
		"appName":     appName,
		"packageName": packageName,
		"updateDesc":  updateDesc,
	}
	appInfoJson, _ := json.Marshal(appInfo)
	// pushInfo := AppPushInfo{
	// 	UserName:    global.Config.App.XiaoMi.UserName,
	// 	SynchroType: "1", // 更新应用
	// 	AppInfo:     string(appInfoJson),
	// }
	pushInfo := map[string]string{
		"userName":    global.Config.App.XiaoMi.UserName,
		"synchroType": "1", // 更新应用
		"appInfo":     string(appInfoJson),
	}
	fileParam := map[string]string{
		"apk": utils.GetFileMd5(apkFile),
		// "icon": utils.GetFileMd5("D:\\bjy\\xiaomi\\logo.png"),
	}
	files := map[string]string{
		"apk": apkFile,
		// "icon": "D:\\bjy\\xiaomi\\logo.png",
	}
	// 签名
	sign, _ := sign(pushInfo, fileParam)
	// 请求参数
	paramJson, _ := json.Marshal(pushInfo)
	requestMap := map[string]string{
		"RequestData": string(paramJson),
		"SIG":         sign,
	}
	utils.Info("请求参数：%v", requestMap)
	result := utils.PostFormFilesHeader(getApiUrl(api_push), requestMap, getHeaders(), files)
	// 结果处理
	var pushResult AppPushResult
	json.Unmarshal([]byte(result), &pushResult)
	if pushResult.Result != 0 {
		return false, pushResult.Message
	}
	return true, pushResult.Message
}
